package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.dao.MerchantUsersMapper;
import com.woniuxy.pojo.MerchantUsers;
import com.woniuxy.service.SeateService;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.LocalTCC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeateServiceImpl implements SeateService {
    @Autowired
    private MerchantUsersMapper merchantUsersMapper;
    @Override
    public void updatebalence(MerchantUsers merchantUsers) {

        if (merchantUsers.getBalance()<merchantUsers.getCashAdvance()){
            throw new RuntimeException("资金不足");
        }
        merchantUsersMapper.updatebalence(merchantUsers);
    }

    @Override
    public Boolean Confirm(BusinessActionContext context) {
        System.out.println("order commit xid "+context.getXid());
        JSONObject jsonObject=(JSONObject) context.getActionContext("user");
        MerchantUsers merchantUsers=jsonObject.toJavaObject(MerchantUsers.class);
        MerchantUsers merchantUsers1=merchantUsersMapper.selectByPrimaryKey(merchantUsers.getId());
        if (merchantUsers1==null){
            return true;
        }
        merchantUsersMapper.Confirm(merchantUsers);
        return true;
    }

    @Override
    public Boolean Rollback(BusinessActionContext context) {
        System.out.println("回滚+++++++++++++++");
        System.out.println("order commit xid "+context.getXid());
        JSONObject jsonObject=(JSONObject) context.getActionContext("user");
        MerchantUsers merchantUsers=jsonObject.toJavaObject(MerchantUsers.class);
        merchantUsersMapper.rollback(merchantUsers);
        return true;
    }
}
